<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class Care_model extends CI_Model
{
    function select_care_lise($param = array(
        'repairid' => '',
        'repairname' => '',
        'start' => 0,
        'length' => 10))
    {

        if ($param['repairid']) {
            $this->db->where('repairid', $param['repairid']);
        }

        if ($param['repairname']) {
            $this->db->like('repairname', $param['repairname']);
        }

        $data['count'] = $this->db->count_all_results('car_repair');

        $this->db->from('car_repair');
        $this->db->order_by("repairid", "asc");
        $this->db->limit($param['length'], $param['start']);

        if ($param['repairid']) {
            $this->db->where('repairid', $param['repairid']);
        }

        if ($param['repairname']) {
            $this->db->like('repairname', $param['repairname']);
        }

        $query = $this->db->get();

        $data['list'] = $query->result_array();

        return $data;

    }


    function insert()
    {
        $data = array(
            'repairname' => $this->input->post('repairname'),
            'repairtime' => $this->input->post('repairtime'),
            'repairmileage' => $this->input->post('repairmileage'),
            'repairtimetype' => $this->input->post('repairtimetype')
        );

        $this->db->insert('car_repair', $data);
        $repairid = $this->db->insert_id();

        $faultids = $this->input->post('faultid');

        foreach ($faultids as $faultid) {
            $data2 = array(
                'repairid' => $repairid,
                'faultid' => $faultid
            );

            $this->db->insert('car_repair_fault', $data2);
        }

    }

    function delete()
    {
        $repairid = $this->input->get_post('repairid');

        $this->db->where('repairid', $repairid);
        $this->db->delete('car_repair');

        $this->db->where('repairid', $repairid);
        $this->db->delete('car_repair_fault');
    }

    function select_care_fault_list()
    {
        $this->db->from('mc_car_fault');
        $this->db->order_by("faultid", "asc");
        $query = $this->db->get();
        return $query->result_array();
    }

    function select_care_by_id()
    {
        $repairid = $this->input->get_post('repairid');

        $this->db->where('repairid', $repairid);
        $query = $this->db->get('car_repair');
        $data['info'] = $query->row_array();

        $this->db->where('repairid', $repairid);
        $query = $this->db->get('car_repair_fault');
        $data['faultids'] = $query->result_array();

        return $data;
    }

    function update()
    {
        $repairid = $this->input->get_post('repairid');

        $fields = array(
            'repairname' => $this->input->post('repairname'),
            'repairtime' => $this->input->post('repairtime'),
            'repairmileage' => $this->input->post('repairmileage'),
            'repairtimetype' => $this->input->post('repairtimetype')
        );

        $this->db->where('repairid', $repairid);
        $this->db->update('car_repair', $fields);

        $this->db->where('repairid', $repairid);
        $this->db->delete('car_repair_fault');

        $faultids = $this->input->post('faultid');
        if ($faultids) {
            foreach ($faultids as $faultid) {
                $data2 = array(
                    'repairid' => $repairid,
                    'faultid' => $faultid
                );

                $this->db->insert('car_repair_fault', $data2);
            }

        }


    }


}